package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.exception.Assert;
import com.atguigu.srb.base.result.R;
import com.atguigu.srb.base.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.Dict;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-01-04
 */
@RestController
@RequestMapping("/api/core/dict")
public class ApiDictController {
    @Resource
    DictService dictService;
    @ApiOperation("根据dictCode查询下一级数据字典")
    @GetMapping("dicts/{dictCode}")
    public R getDictsByDictCode(@PathVariable("dictCode")String dictCode){
        //根据dictCode查询数据字典id
        Dict dict = dictService.getOne(Wrappers.lambdaQuery(Dict.class)
                .eq(Dict::getDictCode, dictCode));
        //根据数据字典id 查询下一级数据字典集合
        Assert.assertObjIsNull(dict , ResponseEnum.ERROR);

        List<Dict> dicts = dictService.list(Wrappers.lambdaQuery(Dict.class)
                .eq(Dict::getParentId, dict.getId()));
        return R.ok().data("items" , dicts);
    }

}

